from datetime import timedelta, datetime
from multiprocessing import Process
import threading
import happybase

lis = [i for i in range(0, 100) if i % 10 == 0]


def insert(start):
    # hb = happybase.Connection(host='117.88.45.156', port=9091)
    # hb = happybase.Connection(host='10.0.17.17', port=8091)
    hb = happybase.Connection(host='localhost', port=8091)
    table = hb.table('xkl_test')
    with table.batch(batch_size=500) as t:
        fmt_s = '%Y%m%d%H%M%S'
        for j in range(start + 1, start + 11):
            print(f'开始第{j}个设备')
            device_id = str(10000000 + j)
            tm = datetime(2022, 3, 22, 0, 0)
            for i in range(2880):
                tm_res = str(tm.strftime(fmt_s))[::-1]
                tm_res2 = str(tm.strftime(fmt_s))
                row_key = tm_res + device_id + 'mi15'
                data = {'cf:device_id': device_id, 'cf:data_time': tm_res2,
                        'cf:efaa_actual_kwh': '1',
                        'cf:efab_actual_kwh': '1',
                        'cf:efac_actual_kwh': '1',
                        'cf:efat_actual_kwh': '1',
                        'cf:efrt_actual_kvarh': '1',
                        'cf:eraa_actual_kwh': '1',
                        'cf:erab_actual_kwh': '1',
                        'cf:erac_actual_kwh': '1',
                        'cf:erat_actual_kwh': '1',
                        'cf:errt_actual_kvarh': '1',
                        'cf:ds_origin_kw': '1',
                        'cf:dp_origin_kw': '1',
                        'cf:df_origin_kw': '1',
                        'cf:do_origin_kw': '1',
                        'cf:dt_origin_kw': '1',
                        'cf:indfaa_origin_kwh': '1',
                        'cf:indfab_origin_kwh': '1',
                        'cf:indfac_origin_kwh': '1',
                        'cf:indfat_origin_kwh': '1',
                        'cf:indfrt_origin_kvarh': '1',
                        'cf:indraa_origin_kwh': '1',
                        'cf:indrab_origin_kwh': '1',
                        'cf:indrac_origin_kwh': '1',
                        'cf:indrat_origin_kwh': '1',
                        'cf:indrp1_origin_kvarh': '1',
                        'cf:indrp2_origin_kvarh': '1',
                        'cf:indrp3_origin_kvarh': '1',
                        'cf:indrp4_origin_kvarh': '1',
                        'cf:indrrt_origin_kvarh': '1',
                        'cf:dt_time_empty': '1',
                        'cf:ds_time_empty': '1',
                        'cf:dp_time_empty': '1',
                        'cf:df_time_empty': '1',
                        'cf:do_time_empty': '1',
                        'cf:ua_origin_v': '1',
                        'cf:ub_origin_v': '1',
                        'cf:uc_origin_v': '1',
                        'cf:ia_origin_a': '1',
                        'cf:ib_origin_a': '1',
                        'cf:ic_origin_a': '1',
                        'cf:pa_actual_kw': '1',
                        'cf:pb_actual_kw': '1',
                        'cf:pc_actual_kw': '1',
                        'cf:pt_actual_kw': '1',
                        'cf:qa_actual_kvar': '1',
                        'cf:qb_actual_kvar': '1',
                        'cf:qc_actual_kvar': '1',
                        'cf:pfa_origin_empty': '1',
                        'cf:pfb_origin_empty': '1',
                        'cf:pfc_origin_empty': '1',
                        'cf:pft_origin_empty': '1',
                        'cf:qt_actual_kvar': '1',
                        'cf:iz_origin_a': '1',
                        'cf:uaa_origin_d': '1',
                        'cf:uab_origin_d': '1',
                        'cf:uac_origin_d': '1',
                        'cf:iaa_origin_d': '1',
                        'cf:iab_origin_d': '1',
                        'cf:iac_origin_d': '1',
                        'cf:ta_origin_dc': '1',
                        'cf:tb_origin_dc': '1',
                        'cf:tc_origin_dc': '1',
                        'cf:paa_origin_kw': '1',
                        'cf:pab_origin_kw': '1',
                        'cf:pac_origin_kw': '1',
                        'cf:iub_origin_d': '1',
                        'cf:uub_origin_d': '1',
                        'cf:data_source': '1', 'cf:frequency': 'mi15'}
                t.put(row_key, data)
                tm += timedelta(minutes=15)
    hb.close()


if __name__ == '__main__':
    # 多进程运行
    # time_start = datetime.now()
    # process_list = []
    # for i in lis:  # 开启5个子进程执行fun1函数
    #     p = Process(target=insert, args=(i,))  # 实例化进程对象
    #     p.start()
    #     process_list.append(p)
    # for i in process_list:
    #     i.join()
    # time_end = datetime.now()
    # res = time_end - time_start
    # print(res)

    # 多线程运行
    thread_list = []
    for i in lis:
        t = threading.Thread(target=insert, args=(i,))
        t.start()
        thread_list.append(t)
    for i in thread_list:
        i.join()
